<?php
/**
 * 产品管理
 *
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class ProductPackageController extends BaseController {

	/*根据产品id获取对应产品线*/
	public function getLineCode(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$product_id = I('get.product_id',false,'intval');
		$wheres = array();
		$wheres['product_id'] = array('eq',$product_id);
		$product_line_code = M('product')->where( $wheres )->getField("product_line_code");
		if($product_line_code){
			$res['data'] = $product_line_code;
			$res['rcode'] = 1;
		}else{
			$res['msg'] = '不存在对应产品线！';
		}
		e_json($res);
	}

	/*获取产品线数据*/
	public function getProductLine(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$productLine = M('product_line')->select();
		if($productLine){
			//搜索条件中添加“所有产品”
			for ($j=0; $j < count($productLine); $j++) {
				$newproduct[$j+1] = $productLine[$j];
			}
			$newproduct[0]['name'] = '所有产品线';
			$newproduct[0]['product_line_code'] = -1;
		}
		if( $productLine ){
			$res['rcode'] = 1;
			$res['data'] = $newproduct;
		}else{
			$res['msg'] = "产品线信息为空";
		}
		e_json( $res );
	}

	/*获取产品数据*/
	public function getProduct(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$product_line_code = I('get.product_line_code');
		$wheres = array();
		$wheres['line.is_deleted']  = array('neq',1);
		if($product_line_code!=-1)
			$wheres['product.product_line_code']  = array('eq',$product_line_code);
		$product = M('product')->field("product.*,line.name as line_name")
				->join("LEFT JOIN product_line line on line.product_line_code=product.product_line_code")
				->where($wheres)
				->order("product.product_id")
				->select();
		for ($i=0; $i < count($product); $i++) {
			$productWithLine[$i] = $product[$i];
			//添加、编辑中产品名称添加对应产品线
			if($product[$i]['name'] && $product[$i]['line_name'])
				$productWithLine[$i]['name'] = $product[$i]['line_name'].'--'.$product[$i]['name'];
		}
		//搜索条件中添加“所有产品”
		for ($j=0; $j < count($product); $j++) {
			$newproduct[$j+1] = $product[$j];
			$newproduct[0]['name'] = '所有产品';
			$newproduct[0]['product_id'] = -1;
		}
		if( $product ){
			$res['rcode'] = 1;
			$res['data'] = $newproduct;
			$res['productWithLine'] = $productWithLine;
		}else{
			$res['msg'] = "产品线信息为空";
		}
		e_json( $res );
	}

	/*列表*/
	public function index(){
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$psize = I('get.psize', $this->m_psize,'intval');
		$name = I('get.name','','trim');
		$product_line = I('get.product_line');
		$product_id = I('get.product_id');

		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;

		$wheres = array();
		if($name)	$wheres['package.name'] = array('like', '%'.$name.'%');
		if($product_line!=-1)	
			$wheres['package.product_line_code'] = array('eq', $product_line);
		if($product_id!=-1)
			$wheres['package.product_id'] = array('eq', $product_id);
		$wheres['package.is_deleted'] = array('neq', 1);
		$wheres['line.is_deleted'] = array('neq', 1);

		$cnt = M("product_package")->alias('package')
						->join("LEFT JOIN product on package.product_id=product.product_id")
						->join("LEFT JOIN product_line line on line.product_line_code=package.product_line_code")
						->where( $wheres )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$productpackage_list = M("product_package")->alias('package')
						->field("package.*,product.name as product_name,line.name as line_name")
						->join("LEFT JOIN product on package.product_id=product.product_id")
						->join("LEFT JOIN product_line line on line.product_line_code=package.product_line_code")
						->where($wheres)
						->order("package.product_id desc")
			            ->limit($Page->firstRow.','.$Page->listRows )
			            ->select();
		$sql = M("product_package")->getLastSql();	        
		for ($j=0; $j < count($productpackage_list); $j++) {
			if($productpackage_list[$j]['package_multi_prices'] && strstr($productpackage_list[$j]['package_multi_prices'], '|')){
				$productpackage_list[$j]['package_multi_prices'] = str_replace('|', ' ; ', $productpackage_list[$j]['package_multi_prices']);
			}
		}
		if( $productpackage_list ){
  			$res['rcode'] = 1;
  			$res['msg'] = "OK";
  			$res['data'] = $productpackage_list;
  			$res['pall'] =  $Page->getTotalPages();
  			$res['p'] = $Page->nowPage;
		}else{
	       	$res['rcode'] = 2;
			$res['msg'] = "产品线信息列表为空";
		}
		$res['sql'] = $sql;
		e_json( $res );
	}

	

	/*添加套餐*/
	public function addpackage(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$add_data = array();
		$add_data['name'] = I('post.add_name','','trim');
		$add_data['product_id'] = I('post.product_id',false,'intval');
		$add_data['package_multi_prices'] = I('post.add_package_price');
		$add_data['package_service'] = I('post.add_package_service',false,'intval');	//服务时长
		$add_data['package_remark'] = I('post.package_remark','','trim');	//备注
		$addService = I('post.addService');
		$addres = D('ProductPackage')->add_package($add_data, $addService);
		$res['rcode'] = $addres['r'];
		$res['data'] = $addres['d'];
		$res['msg'] = $addres['i'];
		e_json( $res );
	}

	/*判断同一产品下，套餐名是否重名*/
	public function checkNameExist(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$product_id = I('post.product_id');
		$name = I('post.name','','trim');
		$package_code = I('post.package_code');
		$wheres = array();
		$wheres['product_id'] = array('eq',$product_id);
		$wheres['name'] = array('eq',$name);
		$wheres['is_deleted'] = array('neq',1);
		if($package_code)	//编辑页面功能
			$wheres['package_code'] = array('neq',$package_code);	
		$nameExist = M("product_package")->where($wheres)->find();
		if( $nameExist ){
			$res['rcode'] = 1;
		}else{
			$res['msg'] = "该套餐名可以添加！";
		}
		e_json( $res );
	}

	/*判断套餐code是否重复*/
	public function checkCodeExist(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$package_name = I('post.package_name');
		$DPackage = D("ProductPackage");
		//过滤特殊字符获取首字母字符
		$name =  $DPackage->replace_specialChar($package_name);
		$package_code = $DPackage->getLowerName($name);
		$wheres = array();
		$wheres['is_deleted'] = array('neq',1);
		$packageList = M("product_package")->where($wheres)->select();
		for ($i=0; $i < count($packageList); $i++) {
			if($packageList[$i]['package_code'] == $package_code){
				$res['rcode'] = 1;
				$res['msg'] = "package exist";
				e_json($res);
			}
		}
		$res['package_code'] = $package_code;
 		e_json( $res );
	}

	/*编辑套餐*/
	public function editPackage(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MProductPackage = D('ProductPackage');
		$edit_data = array();
		$edit_data['package_code'] = I('post.update_package_code');
		$edit_data['name'] = I('post.edit_name','','trim');
		$edit_data['package_multi_prices'] = I('post.edit_package_price');
		$edit_data['package_service'] = I('post.edit_package_service');
		$edit_data['package_remark'] = I('post.package_remark','','trim');
		$edit_data['product_id'] = I('post.product_id',false,'intval');
		$edit_data['editService'] = I('post.selected_service');
		$editres = $MProductPackage->update_Package($edit_data);
		$res['rcode'] = $editres['r'];
		$res['data'] = $editres['d'];
		$res['msg'] = $editres['i'];
		e_json( $res );
	}

	/*删除套餐*/
	public function delPackage(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$package_code = I('get.package_code');
		$qy_deparment_id = I('get.qy_deparment_id',false,'intval');
		if( !$package_code ){
			$res['msg'] = '参数错误';
		}else{
			$MProductPackage = D('ProductPackage');
			$delres = $MProductPackage->delete_Package( $package_code, $qy_deparment_id );
			$res['rcode'] = $delres['r'];
			$res['msg'] = $delres['i'];
		}
		e_json( $res );
	}


	/*获取企业号服务信息*/
	public function getQyService(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$wheres = array();
		$wheres['is_deleted'] = array('neq',1);
		$qyServiceList = M("qy_service")->where($wheres)->select();
		if($qyServiceList){
			$res['data'] = $qyServiceList;
			$res['rcode'] = 1;
		}else{
			$res['msg'] = 'no date';
		}
		e_json($res);
	}

	/*编辑界面获取套餐对应服务列表*/
	public function getServOf(){
		$res = array('rcode'=>0,'qyServiceList'=>NULL, 'msg'=> 'OK');
		$package_code = I('get.package_code');
		$wheres1 = array();
		$wheres1['is_deleted'] = array('neq',1);
		$qyServiceList = M("qy_service")->where($wheres1)->select();
		if(!$package_code){
			$res['msg'] = '参数错误';
		}else{
			$wheres = array();
			$wheres['package_code'] = array('eq',$package_code);
			$findList = M("package_service")->where($wheres)->select();
			for ($i=0; $i < count($qyServiceList); $i++) { 
				for ($j=0; $j < count($findList); $j++) { 
					if($findList[$j]['service_id'] == $qyServiceList[$i]['service_id']){
						$qyServiceList[$i]['check'] = "true";
						$selected_service[] = $findList[$j]['service_id'];
					}
				}
			}
			if( $findList ){
				$res['selected_service'] = $selected_service;
			}else{
				$res['msg'] = 'no select';
			}
			$res['qyServiceList'] = $qyServiceList;
			$res['rcode'] = 1;
		}
		e_json($res);
	}




}
